﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class frmSuaUngHoVatChat : Form
    {
        public frmSuaUngHoVatChat()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        //DataTable dt;
        private void loaddata()
        {
            cobHoTenMTQ.DataSource = cn.getDataTable("tblMTQ_DS");
            cobHoTenMTQ.DisplayMember = "HoTen";
            cobHoTenMTQ.ValueMember = "MaMTQ";

            //cobHienVat.DataSource = cn.getDataTable("tblDungCu_NguyenLieu_DS");
            //cobHienVat.DisplayMember = "TenDungCu";
            //cobHienVat.ValueMember = "MaDungCu";

            cobNguoiNhan.DataSource = cn.getDataTable("tblNhanVien_DS");
            cobNguoiNhan.DisplayMember = "HoTen";
            cobNguoiNhan.ValueMember = "MaNV";
            SqlCommand cmd = new SqlCommand("tblUngHo_DS",cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("stt",SqlDbType.Int)).Value = bientinhSoVang.Sttsovang;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gridChiTietUngHo.DataSource = dt;

            (gridChiTietUngHo.Columns["cbMaDungCu"] as DataGridViewComboBoxColumn).DataSource = cn.getDataTable("tblDungCu_NguyenLieu_DS");
            (gridChiTietUngHo.Columns["cbMaDungCu"] as DataGridViewComboBoxColumn).DisplayMember = "TenDungCu";
            (gridChiTietUngHo.Columns["cbMaDungCu"] as DataGridViewComboBoxColumn).ValueMember = "MaDungCu";

        }
        private void frmSuaUngHoVatChat_Load(object sender, EventArgs e)
        {
            loaddata();
            cobNguoiNhan.Text = bientinhSoVang.Hotennv;
            cobHoTenMTQ.SelectedValue = bientinhSoVang.Mamtq;
            dtNgayUngHo.Value = bientinhSoVang.Ngayungho;           
        }

        private void gridChiTietUngHo_SelectionChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    cobHienVat.SelectedValue = gridChiTietUngHo.CurrentRow.Cells["MaDungCu"].Value;
            //    txtSoLuong.Text = gridChiTietUngHo.CurrentRow.Cells["sl"].Value.ToString();
            //}
            //catch
            //{
            //}
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("tblSoVang_Sua",cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("stt",SqlDbType.Int)).Value = bientinhSoVang.Sttsovang;
            cmd.Parameters.Add(new SqlParameter("mamtq", SqlDbType.Int)).Value = cobHoTenMTQ.SelectedValue;
            cmd.Parameters.Add(new SqlParameter("matv", SqlDbType.VarChar)).Value = cobNguoiNhan.SelectedValue;
            cmd.Parameters.Add(new SqlParameter("ngayungho", SqlDbType.DateTime)).Value = dtNgayUngHo.Value;
            if (cmd.ExecuteNonQuery() == 1)
            {

                int i;
                for (i = 0; i < gridChiTietUngHo.Rows.Count; i++)
                {                    
                    DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)gridChiTietUngHo.Rows[i].Cells["cbMaDungCu"];
                    int madungcu = int.Parse(c.Value.ToString());
                    double soluong = double.Parse(gridChiTietUngHo.Rows[i].Cells["sl"].Value.ToString());
                    int id = Convert.ToInt32(gridChiTietUngHo.Rows[i].Cells["id"].Value.ToString());
                    SqlCommand cmd_insert = new SqlCommand("tblUngHo_Sua", cn.ketnoi());
                    cmd_insert.CommandType = CommandType.StoredProcedure;
                    cmd_insert.Parameters.Add(new SqlParameter("madungcu", SqlDbType.Int)).Value = madungcu;
                    cmd_insert.Parameters.Add(new SqlParameter("soluong", SqlDbType.Int)).Value = soluong;
                    cmd_insert.Parameters.Add(new SqlParameter("id", SqlDbType.Int)).Value = id;
                    cmd_insert.ExecuteNonQuery();                    
                }
            }
            MessageBox.Show("Thành công","Thông Bá",MessageBoxButtons.OK,MessageBoxIcon.Information);
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int r = this.gridChiTietUngHo.CurrentRow.Index;
            gridChiTietUngHo.AllowUserToDeleteRows = true;
        }

        private void gridChiTietUngHo_SelectionChanged_1(object sender, EventArgs e)
        {
            try
            {
                gridChiTietUngHo.CurrentRow.Selected = true;
            }
            catch
            {
            }
        }

        private void btnThemMTQ_Click(object sender, EventArgs e)
        {
            frmThemMTQ frm = new frmThemMTQ();
            frm.ShowDialog();
            loaddata();
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            loaddata();
        }
        

      }
}
